<?php

namespace app\index\home;
use think\Controller;
use think\Db;
use api\ServerResponse;
class Index extends Controller
{
    public function initialize() {

    }
    public function index()
    {
    	// phpinfo();
        echo '请用微信打开访问';
    }
    /**
     * 获取首页图片 function
     *
     * @return void
     */
    public function getIndexPic()
    {
        $pic = config('base.index_pic');
        $data = [
            'code' => true,
            'data' => $this->request->domain().$pic
        ];
        return json($data);
    }

    /**
     * 获取图片分类 function
     *
     * @return void
     */
    public function getPicCateList()
    {
        $model = Db::name('pic_cate')->alias('pc');
        $page       = $this->request->param('page/d', 1);
        $limit      = $this->request->param('limit/d', 5);
        $where = [];
        $data['data'] = $model->where($where)->page($page)->limit($limit)->order('sort asc')->select();
        $data['count'] = $model->where($where)->count('id');
        $data['code'] = true;
        $data['msg'] = '';
        return json($data);
    }
    /**
     * 获取图片列表 function
     *
     * @return void
     */
    public function getPiclist()
    {
        $where = [];

        $page       = $this->request->param('page/d', 1);
        $limit      = $this->request->param('limit/d', 6);
        $cate_id    = $this->request->param('cate_id/d', '');
        $author_id  = $this->request->param('author_id', '');
        if(!empty($author_id)){

            $info = checkToken($author_id);
            // halt($info);
            if(!$info){
                $data['code'] = false;
                $data['msg'] = '请重新登录后操作';
                return json($data);
            }

             $where[] = ['author_id','eq',$info->uid];
        }
        if(!empty($cate_id)){
            $where[] = ['cate_id','eq',$cate_id];
        }

        $model = Db::name('pic')->alias('p');

        if($cate_id == 5){

            $where = [];
            $where[] = ['cate_id','eq',4];
            $field = 'p.*,pc.name as cate_name,u.nickname,p.hot';
            $data['data'] = $model->field($field)
                                ->join('pic_cate pc','pc.id=p.cate_id')
                                ->join('users u','u.id=p.author_id')
                                ->where($where)
                                // ->page($page)
                                ->limit(10)
                                ->order('hot desc')
                                ->select();
        }else{
            $field = 'p.*,pc.name as cate_name,u.nickname';
            $data['data'] = $model->field($field)
                                ->join('pic_cate pc','pc.id=p.cate_id')
                                ->join('users u','u.id=p.author_id')
                                ->where($where)
                                // ->page($page)
                                // ->limit($limit)
                                ->order('p.sort acs,p.id desc')
                                ->select();
        }


        foreach ($data['data'] as $key => $value) {
            $data['data'][$key]['vote'] = 0;
            if($value['cate_id'] == 4){
                $data['data'][$key]['vote'] = Db::name('vote_log')->where('pic_id',$value['id'])->count('id');
            }
        }
        $data['count'] = $model->where($where)->count('p.id');
        $data['code'] = true;
        $data['msg'] = '';
        return json($data);
    }

    public function getPicInfo()
    {
        $where = [];
        $id = $this->request->param('id');
        $field = 'p.*,pc.name as cate_name,u.nickname';
        $model = Db::name('pic')->alias('p');
        $where[] = ['p.id','eq',$id];
        $info = $model->field($field)
                ->join('pic_cate pc','pc.id=p.cate_id')
                ->join('users u','u.id=p.author_id')
                ->where($where)->find();
        if(!$info){
            $data['code'] = false;
            $data['msg'] = '请求参数错误';
            return json($data);
        }
        $data['data'] = $info;
        $data['code'] = true;
        $data['msg'] = '';
        return json($data);
    }

    /**
     * [vote 投票]
     * @return [type] [description]
     */
    public function vote()
    {
        $token = $this->request->param('token');
        try {
            $info = checkToken($token);
        } catch (\Exception $e) {
            $data['code'] = false;
            $data['msg'] = '请重新登录后操作';
            return json($data);
        }
        $postData = $this->request->post();
        $data = [
            'pic_id'     => $postData['pic_id'],
            'user_id'    => $info->uid,
            'create_time'=> time(),
            'unique_str' => md5($postData['pic_id'].$info->uid.date('Y-m-d',time())),
        ];
        try {
            $res = Db::name('vote_log')->insert($data);
            Db::name('pic')->where('id',$postData['pic_id'])->setInc('hot');
        } catch (\Exception $e) {
            $returnData['code'] = false;
            $returnData['msg'] = '每人每天每图只限投票一次!';
            return json($returnData);
        }
        if($res){
            $returnData['code'] = true;
            $returnData['msg'] = '投票成功!';
            $returnData['vote'] = Db::name('vote_log')->where('pic_id',$postData['pic_id'])->count('id');
            return json($returnData);
        }

    }
}
